<?
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  
  //================ PAGE LOGIC ================
  $alert=getAlert();
  
  if ($_GET["cmd"]=="del") {
    $ret=$asterisk->delZapataCard($_GET["id"]);
    if ($ret==1) $alert="<div class=\"AlertGood\">".$cwlang["zapata"]["alertDelGood"]."</div>";
    else $alert="<div class=\"AlertBad\">".$cwlang["zapata"]["alertDelBad"]."</div>";
    goToMainPage($alert,"index.php");
  }

  // install a new card or update the port parameters

  if (count($_POST)>0) {
    if ($_POST["act"]=="install") {
      $card=$_POST["card"];
      $ret=$asterisk->putZapataCard($card);
      if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["zapata"]["alertAddGood"]."</div>";
      else $alert="<div class=\"AlertBad\">".$cwlang["zapata"]["alertAddBad"]."</div>";
    } else {
      $entry=Array();
      $card=$_POST["card"];
      foreach($_POST["type"] as $id=>$l) {
        if ($_POST["crc4"][$id]   != 1) $crc4=0;   else $crc4=1;
        if ($_POST["yellow"][$id] != 1) $yellow=0; else $yellow=1;
        $entry[]=Array("id"     =>$id, 
                       "type"   =>$_POST["type"][$id],
                       "device" =>$_POST["device"][$id],
                       "timing" =>$_POST["timing"][$id],
                       "lbo"    =>$_POST["lbo"][$id],
                       "framing"=>$_POST["framing"][$id],
                       "coding" =>$_POST["coding"][$id],
                       "crc4"   =>$crc4,
                       "yellow" =>$yellow);
      }
      if ($_POST["loadzone"]) {
        foreach($_POST["loadzone"] as $id=>$l)
          $zones[]=Array("id"=>$id, "loadzone"=>$l);
      }
      $zones[]=Array("id"=>$id, "defzone"=>$_POST["defzone"]);

      $ret=$asterisk->updateZapataPort($entry,$zones);
      if ($ret>0) $alert="<div class=\"AlertGood\">".$cwlang["zapata"]["alertSaveGood"]."</div>";
      else $alert="<div class=\"AlertBad\">".$cwlang["zapata"]["alertSaveBad"]."</div>";
    }
    goToMainPage($alert,"index.php");
  } // End install or update card, port and loadzone, defautlzone


  // create the web interface

  $ret=$asterisk->getZapataPort(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  $old_card_id=0;
  $count=1;
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id=$v["id"];
      $port=$v["port"];
      $type=$v["type"];
      $device=$v["device"];
      $span=$v["span"];
      $timing=$v["timing"];
      $lbo=$v["lbo"];
      $framing=$v["framing"];
      $coding=$v["coding"];
      $crc4=$v["crc4"];
      $yellow=$v["yellow"];
      $order=$v["order"];
      $card=$v["card"];
      $card_id=$v["card_id"];
      if ($old_card_id!=$card_id) {
        if ($count>1) $html.="<tr><td colspan=9>&nbsp;</td></tr>\n";
        $c=$asterisk->getZapataCard($card);
        $name=$c[0]["name"];
        $show=split(',',$c[0]["type"]); // Need to be changed with a
                                        // getZapataOptions routine
        $html.="<table class=\"todolist\" cellpadding=\"0\" cellspacing=\"0\" border=0><tr><td colspan=9><span style='color: green; font-weight: bold;'>".$cwlang["zapata"]["card"]." No. $card_id:</span> $name  <a href=\"$PHP_SELF?cmd=del&id=$card_id\" onclick=\"if(confirm('".$cwlang['zapata']['delalert']."')){document.getElementById('tplpbxsytatus').src='".$relBasePath."/public/img/tl-red-small.gif'; return true;} else return false;\" ><img src=\"$relBasePath/public/img/trash.gif\" /></a></td></tr>\n";
        //$html.="<tr><td colspan=10><span style='color: green; font-weight: bold;'>".$cwlang["zapata"]["card"]." No. $card_id:</span> $name  <a href=\"$PHP_SELF?cmd=del&id=$card_id\" onclick=\"if (confirm('Are you sure to remove the card?')) return true; else return false;\"><img src=\"$relBasePath/public/img/trash.gif\" /></a></td><colspan 10></tr>\n";

        // Print Header for each card

	// If port type is E1/T1 print span header
        if($show[0]=="E1" or $show[0]=="T1")
        {
          $html.="
	<tr>
	<td width=80 align=left>&nbsp;
	<td>Type
	<td>Protocol
	<td>Timing
	<td>LBO
	<td>Framing
	<td>Coding
	<td>CRC4
	<td>Yellow
	</tr>";
        }
	// If port type is FXO/FXS this one
        else if($show[0]=="FXO" or $show[0]=="FXS")
        { 
          $html.="
	<tr>
	<td width=80>&nbsp;
	<td width=51>Type
	<td width=51>Protocol
	<td width=51>&nbsp;
	<td width=51>&nbsp;
	<td width=51>&nbsp;
	<td width=51>&nbsp;
	<td width=51>&nbsp;
	<td width=51>&nbsp;
	</tr>";
        }
	// If port type is unknown 
        else 
        {
          $html.="
	<tr>
	<td width=80 align=left>&nbsp;
	<td>Type
	<td>&nbsp;
	<td>&nbsp;
	<td>&nbsp;
	<td>&nbsp;
	<td>&nbsp;
	<td>&nbsp;
	<td>&nbsp;
	</tr>";}
        }

        // First do the port type selection

        $choose_type     = $show;
        $option_type     = "";
        foreach ($choose_type as $c) {
          if ($c==$type) $chk_opt=" selected "; else $chk_opt="";
            $option_type.="<option $chk_opt>".$c;
        }

        $border="valign='top' style='border-bottom: 1px dotted gray;'";
        $html.="<tr><td width=80 align=left $border>";
        $html.="<strong>$id</strong>&nbsp;-&gt;&nbsp;".$cwlang["zapata"]["port"]."&nbsp;"."$port";

        $html.="<td align=left $border><select name=type[$id]> 
        <option>$option_type</select>".$input;     


        $option=$asterisk->getZapataPortOption("$type");
      
        // options to choose from for pulldown

        $choose_device   = split(',',$option[0]["device"]);
        $choose_timing   = split(',',$option[0]["timing"]);
        $choose_lbo      = split(',',$option[0]["lbo"]);
        $choose_framing  = split(',',$option[0]["framing"]);
        $choose_coding   = split(',',$option[0]["coding"]);

        // clear fields

        $option_device   = "";
        $option_timing   = "";
        $option_lbo      = "";
        $option_framing  = "";
        $option_coding   = "";

        foreach ($choose_device as $c) {
          if ($c==$device) $chk_opt=" selected "; else $chk_opt="";
            $option_device.="<option $chk_opt>".$c;
        }
        foreach ($choose_timing as $c) {
          if ($c==$timing) $chk_opt=" selected "; else $chk_opt="";
            $option_timing.="<option $chk_opt>".$c;
        }
        foreach ($choose_lbo as $c) {
          if ($c==$lbo) $chk_opt=" selected "; else $chk_opt="";
            $option_lbo.="<option $chk_opt>".$c;
        }
        foreach ($choose_framing as $c) {
          if ($c==$framing) $chk_opt=" selected "; else $chk_opt="";
            $option_framing.="<option $chk_opt>".$c;
        }
        foreach ($choose_coding as $c) {
          if ($c==$coding) $chk_opt=" selected "; else $chk_opt="";
            $option_coding.="<option $chk_opt>".$c;
        }
        if ($crc4) {
          $status_crc4="checked";
        } else {
          $status_crc4="";
        }
        if ($yellow) {
          $status_yellow="checked";
        } else {
          $status_yellow="";
        }

        // Figure out form display parameter

        if ($type == "E1" || $type == "T1") { 
          $html.="
          <td align=left $border><select name=device[$id]>  <option>$option_device  </select>
          <td align=left $border><select name=timing[$id]>  $option_timing  </select>
          <td align=left $border><select name=lbo[$id]>     $option_lbo     </select>
          <td align=left $border><select name=framing[$id]> <option>$option_framing </select>
          <td align=left $border><select name=coding[$id]>  <option>$option_coding  </select>
          <td align=left $border><input type=checkbox value='1' $status_crc4                   name=crc4[$id]  </input>
          <td align=left $border><input type=checkbox value='1' $status_yellow               name=yellow[$id]> </input>
          ";
        }

        if ($type=='FXS' or $type=='FXO') {
          $html.="  
          <td align=left $border>
          <select name=device[$id]><option>$option_device</select>
          <td align=left $border>&nbsp;
          <input type='hidden' name=timing[$id]>
          <td align=left $border>&nbsp;
          <input type='hidden' name=lbo[$id]>
          <td align=left $border>&nbsp;
          <input type='hidden' name=framing[$id]>
          <td align=left $border>&nbsp;
          <input type='hidden' name=coding[$id]>
          <td align=left $border>&nbsp;
          <input type='hidden' name=crc4[$id]>
          <td align=left $border>&nbsp;
          <input type='hidden' name=yellow[$id]>
          ";
        }

        // $port type not defined 
        if ($type == '') 
        { $html.="
          <td align=left $border>
          <input type='hidden' name=device[$id]>
          <td align=left $border>
          <input type='hidden' name=timing[$id]>
          <td align=left $border>
          <input type='hidden' name=lbo[$id]>
          <td align=left $border>
          <input type='hidden' name=framing[$id]>
          <td align=left $border>
          <input type='hidden' name=coding[$id]>
          <td align=left $border>
          <input type='hidden' name=crc4[$id]>
          <td align=left $border>
          <input type='hidden' name=yellow[$id]>
          ";
        }
      $old_card_id=$card_id;
      $count++;
    }


    // Now the Tonezone 

    if ($ret!="") {
      $ret=$asterisk->getZapataToneZone(0);  
      if (is_array($ret)) {
        $count="0";
        foreach($ret as $v) {
          $id=$v["id"];
          $name=$v["name"];
          $fullname=$v["fullname"];
          $loadzone=$v["loadzone"];
          $defaultzone=$v["defaultzone"];
          if($loadzone!="0"){ $select="checked"; $loadzoneall.=$name.",";} else{ $select="";}
          if($defaultzone!="0"){ $select2="selected";} else{ $select2="";}
          if($count%4==0)$zone.="<tr>";
          $zone.="<td><input name=loadzone[$count] type=checkbox value='$name' $select>$name : $fullname";
          $defzone.="<option value='$name' $select2>$name : $fullname</option>\n";
          $count++; 
        }
      }
    
      $style="style=color:green;font-size:smaller;font-weight:bold";
      $html.="<tr><td colspan=9><br><a href=# onclick=\" return showHide('table_zone')\">LoadZone</a>:&nbsp;".substr($loadzoneall,0,-1)."</td><table class=\"todolist\" id=table_zone bgcolor=#f6f6f6 border=0 cellpadding=0 cellspacing=0 style=display:none>";
      $html.="<br>$zone";
      $html.="</table>";
      $html.="<span $style>Default Zone:&nbsp;</span>";
      $html.="<select name=defzone>$defzone</select>";
    } 
    $html.="</td></tr></table>";
  }  

  $ret=$asterisk->getZapataCard(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) {
    foreach($ret as $v) {
      $id=$v["id"];
      $name=$v["name"];
      $cards.="<option value=\"$id\">$name</option>\n";
    }
  }    
  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <? $sel="cards"; include ("navigation.php");?>
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["zapata"]["title"]?></h1>
  </div>
  <form action="<?=$PHP_SELF?>" method="post">
    <table class="todolist" cellpadding="0" cellspacing="0" border=0>
      <tr>
        <?=$html?>
      </tr>
    </table>
    <?if ($html!="") {?><input type="submit" value="<?=$cwlang["zapata"]["save"]?>" style="font-size: 11px; float: right; margin-right: 20px;" onclick="if(confirm('<?=$cwlang['zapata']['installalert']?>')){document.getElementById('tplpbxsytatus').src='<?=$relBasePath?>/public/img/tl-red-small.gif'; return true;} else return false;" /><?}?>
    <input type="hidden" name="act" value="update" />
  </form>
  
  <br /><br />
  <div id="basic_form">
    <form action="<?=$PHP_SELF?>" method="post">
      <h2><?=$cwlang["zapata"]["newCard"]?></h2>
      <p class="blockintro"><?=$cwlang["zapata"]["newCardDesc"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required" style="text-align: right" ><?=$cwlang["zapata"]["card"]?>:</td>
            <td><select name="card"><?=$cards?></select></td>
          </tr>
          <tr><td></td><td><input type="submit" alt="Salva" value="<?=$cwlang["zapata"]["install"]?>" /></td></tr> 
        </table>
      </div>
      <input type="hidden" name="act" value="install" />
    </form>
  </div>  
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <?$sel="zapata";include("../sidebar.php");?>
          <h1><?=$cwlang["zapata"]["sidebarTitle"]?></h1>
            <?=$cwlang["zapata"]["sidebarDesc"]?>
          <p> 
            <img src="../../../admin/public/img/quad1.jpg" width="200" alt="zapata card" />
          </p>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "zapata");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

